﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Linq;

namespace UniMana.Data
{
    public static class Personnel_Personnel_SessionData
    {
        public static IQueryable GetScheduler(this Table<Personnel_Personnel_Session> nPersonnel_Personnel_Session, int _iSessionKey, int _iTraining_WeekDayKey, Guid _gPersonnel_PersonnelKey, DateTime _dteStart, DateTime _dteEnd, bool _bBusy = false)
        {
            var iQuery = nPersonnel_Personnel_Session.Where(p => p.Training_SessionKey == _iSessionKey && p.Training_WeekDayKey == _iTraining_WeekDayKey && p.Personnel_PersonnelKey == _gPersonnel_PersonnelKey).OrderBy(p => p.RegistTime);
            var iQuery1 = iQuery.Where(p => p.RegistTime >= _dteStart && p.RegistTime <= _dteEnd && p.Status == _bBusy);
            var iLastResult = from query in iQuery1
                              select new
                              {
                                  query.RegistTime,
                                  query.Status
                              };
            return iLastResult;
        }

        public static IQueryable GetSchedulerBySubjectRequestOpen(
            this Table<Personnel_Personnel_Session> nPersonnel_Personnel_Session,
            int _iTraining_SubjectOpenRequestKey)
        {
            var iQuery = nPersonnel_Personnel_Session.Where(
                p => p.Training_SubjectOpenRequestKey ==
                    _iTraining_SubjectOpenRequestKey).OrderBy(p => p.RegistTime);
            var iLastQuery = from query in iQuery
                             select new
                             {
                                 query.RegistTime,
                                 query.Personnel_PersonnelKey
                             };
            return iLastQuery;
        }

        public static void ClearPersonnelSession(this Table<Personnel_Personnel_Session> nPersonnel_Personnel_Session, int _iTraining_SubjectOpenRequestKey)
        {
            DBMain DBMain = new DBMain();
            var iQuery = DBMain.m_DCSave.Personnel_Personnel_Sessions.Where(p => p.Training_SubjectOpenRequestKey == _iTraining_SubjectOpenRequestKey);
            foreach (Personnel_Personnel_Session perperseion in iQuery)
            {
                perperseion.Status = false;
                perperseion.Training_SubjectOpenRequestKey = null;
            }
            DBMain.m_DCSave.SubmitChanges();
        }


        /// <summary>
        /// Lấy danh sách buổi giảng
        /// </summary>
        /// <param name="nPersonnel_Personnel_Session"></param>
        /// <param name="key"></param>
        /// <returns></returns>

        public static IQueryable GetSession(this Table<Personnel_Personnel_Session> nPersonnel_Personnel_Session,
            int key)
        {
            var values = from t in nPersonnel_Personnel_Session
                         where t.Training_SubjectOpenRequestKey == key
                         orderby t.RegistTime ascending
                         select t;
            return values;
        }

        /// <summary>
        /// Lấy dữ liệu các công việc theo thời gian
        /// </summary>
        /// <param name="personsession"></param>
        /// <param name="m_strPersonKey"></param>
        /// <param name="m_dStart"></param>
        /// <param name="m_dEnd"></param>
        /// <returns></returns>

        public static IQueryable GetDataOfPerson(this Table<Personnel_Personnel_Session> nPersonnel_Personnel_Session,
            string m_strPersonKey,
            DateTime m_dStart,
            DateTime m_dEnd)
        {
            var values = (from t in nPersonnel_Personnel_Session
                          where t.Personnel_PersonnelKey.ToString() == m_strPersonKey
                          select new
                          {
                              t.Personnel_PersonnelKey,
                              t.RegistTime,
                              t.Training_WeekDayKey,
                              t.Training_SessionKey,
                              t.Personnel_WorkTypeKey,
                              t.Note,
                              t.Status
                          }).Distinct();

            if (m_dStart.ToString() != "1/1/0001 12:00:00 PM")
            {
                values = values.Where(tb => tb.RegistTime >= m_dStart);
            }
            if (m_dEnd.ToString() != "1/1/0001 12:00:00 PM")
            {
                values = values.Where(tb => tb.RegistTime <= m_dEnd);
            }
            return values.Distinct();
        }

        /// <summary>
        /// Lấy dữ liệu với mã nhân viên
        /// </summary>
        /// <param name="personsession"></param>
        /// <param name="m_strPersonKey"></param>
        /// <returns></returns>

        public static IQueryable GetDatetimeOfPerson(this Table<Personnel_Personnel_Session> nPersonnel_Personnel_Session,
            string m_strPersonKey)
        {
            var values = from p in nPersonnel_Personnel_Session
                         where p.Personnel_PersonnelKey.ToString() == m_strPersonKey
                         select new
                         {
                             p.RegistTime,
                         };
            return values.Distinct();
        }

        /// <summary>
        /// Lấy dữ liệu với mã nhân viên và ngày đăng ký
        /// </summary>
        /// <param name="personsession"></param>
        /// <param name="m_strPersonKey"></param>
        /// <param name="m_dDatetime"></param>
        /// <returns></returns>

        public static IQueryable GetAllData(this Table<Personnel_Personnel_Session> nPersonnel_Personnel_Session,
            string m_strPersonKey,
            DateTime m_dDatetime)
        {
            var values = from p in nPersonnel_Personnel_Session
                         where p.Personnel_PersonnelKey.ToString() == m_strPersonKey
                         where p.RegistTime == m_dDatetime
                         select p;
            return values.Distinct();
        }
    }
}
